package example.gl2jniforceball;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class GameGLSurfaceView extends GLSurfaceView {

    protected Box2dRenderer mRenderer;
    
    public GameGLSurfaceView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    @Override
    public void setRenderer(Renderer r) {
        super.setRenderer(r);
        
        if (r.getClass() == Box2dRenderer.class)
            mRenderer = (Box2dRenderer) r;
        else
            Log.i("GameGLSurfaceView", "setRender: renderer must be Box2dRenderer");
            
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {

        if (mRenderer != null) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                mRenderer.onTouchDown(event.getX(), event.getY());
                return true;

            case MotionEvent.ACTION_UP:
                mRenderer.onTouchUp(event.getX(), event.getY());
                return true;

            case MotionEvent.ACTION_MOVE:
                mRenderer.onTouchMove(event.getX(), event.getY());
                return true;

            }
        } else {
            Log.i("GameGLSurfaceView", "touch:" + event.getAction()
                    + ", renderer is null. Did you call setRenderer?");
        }

        return super.onTouchEvent(event);

    }

    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

}
